package example.controller;

import com.alibaba.fastjson.JSONObject;
import com.mybatis.DAO.registDAO;
import com.mybatis.model.User;
import com.mybatis.model.UserDB;
import com.mybatis.verification.CheckMsg;
import com.mybatis.verification.SendMsg;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import process.Login;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/home")
public class IndexController {

    @RequestMapping("/doLogin")
    public String doLogin() {
        return "login";
    }

    @RequestMapping(value="/login",method= RequestMethod.POST)
    public String login(HttpServletRequest request, String login_name, String login_password) throws Exception{
        //request.getSession().setAttribute("username", username);
        //request.getSession().setAttribute("user", user);
//        System.out.println(login_name);
//        System.out.println(login_password);
//    	LoginAction la = new LoginAction();
//    	la.execute(login_password);
        Login lg = new Login();

        User user = lg.judge(login_name, login_password);
        if(StringUtils.isEmpty(user)) {
            return "login";
        }else {
            System.out.println(user.getName());
            //request.getSession().setAttribute("user", user);
            return "index";
        }

    }

    @RequestMapping("/register")
    public String register() {

        return "registPage";
    }
    @RequestMapping("/registpage")
    public String registpage() {
        return "registPage";
    }

    @RequestMapping(value = "/regist")
    public @ResponseBody String regist(String user_phone, String checkCode,String regist_password){
        int stateCode;
        System.out.println("注册");
        stateCode = CheckMsg.sendtocheck(user_phone,checkCode);

        String result="false";
        switch (stateCode)
        {
            case 0:
                UserDB udb=new UserDB();
                udb.addUser(user_phone,"",user_phone,regist_password,user_phone,"","","");
                result="success";
                break;
            case 1:
                break;
            case 2:
                break;
        }

        return result;
    }
    @RequestMapping("/getCheckCode")
    public @ResponseBody
    String sendCheckcode(HttpServletRequest request, String user_phone){
        System.out.println("发送验证码"+user_phone);
        SendMsg.sendCheckCode(user_phone);

        JSONObject jso=new JSONObject();
        jso.put("stauts","success");
        return jso.toString();
    }
}
